package com.compmov.libraries;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.compmov.datamodel.Persona;
import com.compmov.datamodel.Comentario;
import com.compmov.socialnetwork.Comments;
import com.compmov.socialnetwork.Perfil;
import com.compmov.socialnetwork.PerfilAmigo;
import com.compmov.socialnetwork.R;

public class CommentAdapter extends BaseAdapter {

	private Activity activity;
	private List<Comentario> data;

	private static LayoutInflater inflater=null;
	public ImageLoader imageLoader; 

	public CommentAdapter(Activity a, List<Comentario> d) {
		activity = a;
		data=d;
		inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		imageLoader=new ImageLoader(activity.getApplicationContext());
	}

	public int getCount() {
		return data.size();
	}

	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}

	public static class ViewHolder{
		public TextView textComment;
		public TextView textFecha;
		public ImageView imgRanking;
		public TextView textNombre;     
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		View vi=convertView;
		ViewHolder holder;
		if(convertView==null){
			vi = inflater.inflate(R.layout.item_comentario, null);
			holder=new ViewHolder();
			holder.textComment=(TextView)vi.findViewById(R.id.textComentario);
			holder.textFecha=(TextView)vi.findViewById(R.id.textFecha);;
			holder.imgRanking=(ImageView)vi.findViewById(R.id.imgRan);
			holder.textNombre=(TextView)vi.findViewById(R.id.textNombre);            
			vi.setTag(holder);
		}
		else
			holder=(ViewHolder)vi.getTag();

		Comentario comentario=data.get(position);

		holder.textComment.setText(comentario.getComentario());
		holder.textFecha.setText("Fecha: "+comentario.getFecha());		
		holder.textNombre.setText(""+comentario.getPersona().getNombreCompleto());
		holder.textNombre.setId(comentario.getPersona().getId());		
		holder.textNombre.setOnClickListener(new View.OnClickListener() {

			public void onClick(View view) {
				Intent nuevoIntent = new Intent(activity,
						PerfilAmigo.class);
				nuevoIntent.putExtra("idPer", view.getId());
				activity.startActivity(nuevoIntent);

			}
		});
		int rank=comentario.getRanking();
		 switch(rank){
	        case 1: holder.imgRanking.setImageResource(R.drawable.ranking1); break;
	        case 2: holder.imgRanking.setImageResource(R.drawable.ranking2); break;
	        case 3:holder.imgRanking.setImageResource(R.drawable.ranking3); break;
	        case 4: holder.imgRanking.setImageResource(R.drawable.ranking4); break;
	        default: holder.imgRanking.setImageResource(R.drawable.ranking5); 
	        }
		
		return vi;
	}
	
	 public void setRankingImage(int ranking){
	    	
	       
	    }
}